package com.taxshare.purchaser.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.pager.PageHelp;
import com.taxshare.mybatis.pager.PageInfoRequest;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.purchaser.constant.CodeConstant;
import com.taxshare.purchaser.entity.*;
import com.taxshare.purchaser.mapper.*;
import com.taxshare.purchaser.view.dto.ApplicationRecordDTO;
import com.taxshare.response.beans.Response;
import com.taxshare.tools.token.UserInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author YANG
 * @date 2021/4/9
 * @Description:申请记录
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class ApplicationRecordService extends ServiceImpl<ApplicationRecordMapper, ApplicationRecordEntity> {

    @Autowired
    InvoiceCollectionService invoiceCollectionService;
    @Autowired
    InvoiceCollectionMapper invoiceCollectionMapper;

    @Autowired
    TrainTicketCollectService trainTicketCollectService;
    @Autowired
    TrainTicketMapper trainTicketMapper;
    @Autowired
    TrainTicketPoolMapper trainTicketPoolMapper;


    @Autowired
    TaxiTicketCollectService taxiTicketCollectService;
    @Autowired
    TaxiTicketMapper taxiTicketMapper;
    @Autowired
    TaxiTicketPoolMapper taxiTicketPoolMapper;

    @Autowired
    TollTicketCollectService tollTicketCollectService;
    @Autowired
    TollTicketMapper tollTicketMapper;
    @Autowired
    TollTicketPoolMapper tollTicketPoolMapper;

    @Autowired
    QuotaTicketCollectService quotaTicketCollectService;
    @Autowired
    QuotaTicketMapper quotaTicketMapper;
    @Autowired
    QuotaTicketPoolMapper quotaTicketPoolMapper;

    @Autowired
    BusTicketCollectService busTicketCollectService;
    @Autowired
    BusTicketMapper busTicketMapper;
    @Autowired
    BusTicketPoolMapper busTicketPoolMapper;

    @Autowired
    MachinTicketCollectService machinTicketCollectService;
    @Autowired
    MachinTicketMapper machinTicketMapper;
    @Autowired
    MachinTicketPoolMapper machinTicketPoolMapper;

    @Autowired
    PlaneTicketCollectService planeTicketCollectService;
    @Autowired
    PlaneTicketMapper planeTicketMapper;
    @Autowired
    PlaneTicketPoolMapper planeTicketPoolMapper;

    @Autowired
    ApplicationRecordDetailMapper applicationRecordDetailMapper;

    @Autowired
    InvoicePoolMapper invoicePoolMapper;

    /**
     * 申请记录查询【申请记录】
     *
     * @param query
     * @return
     */
    public Response queryRecord(PageRequest<ApplicationRecordDTO> query) {
        ApplicationRecordDTO applicationRecordDTO = query.getInfo();
        QueryWrapper<ApplicationRecordEntity> recordQuery = new QueryWrapper<>();
        if(CodeConstant.GENERAL_TYPE.equals(UserInfoUtil.getUserType())){
            recordQuery.eq("create_userid", UserInfoUtil.getUserID());
        }
        if(StrUtil.isNotBlank(applicationRecordDTO.getApprovalStatus())){
            recordQuery.eq("approval_status", applicationRecordDTO.getApprovalStatus());
        }
        recordQuery.orderByDesc("create_time");
        IPage<ApplicationRecordEntity> applicationRecordEntityIPage = PageHelp.siamplePage(this, recordQuery, query.getPage(), ApplicationRecordEntity.class);
        return Response.success(applicationRecordEntityIPage);
    }

    /**
     * 驳回审批查询【申请记录】
     *
     * @param query
     * @return
     */
    public Response queryRejectRecord(PageRequest<ApplicationRecordDTO> query) {
        ApplicationRecordDTO applicationRecordDTO = query.getInfo();
        QueryWrapper<ApplicationRecordEntity> recordQuery = new QueryWrapper<>();
        if(StrUtil.isNotBlank(applicationRecordDTO.getApprovalStatus())){
            recordQuery.eq("approval_status", applicationRecordDTO.getApprovalStatus());
        }
        recordQuery.orderByDesc("create_time");
        IPage<ApplicationRecordEntity> applicationRecordEntityIPage = PageHelp.siamplePage(this, recordQuery, query.getPage(), ApplicationRecordEntity.class);
        return Response.success(applicationRecordEntityIPage);
    }


    /**
     * 发票查看【申请记录】
     *
     * @param query
     * @return
     */
    public Response queryDetail(PageRequest<ApplicationRecordDTO> query) {
        ApplicationRecordDTO applicationRecordDTO = query.getInfo();
        PageInfoRequest page = query.getPage();
        String ticketTypes = applicationRecordDTO.getTicketTypes();
        String recordNo = applicationRecordDTO.getRecordNo();
        if (CodeConstant.TICKET_TYPES.equals(ticketTypes)) {
            //增值税发票
            List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = getRecordDetail(recordNo);
            //获取发票代码集合
            List<String> invoiceCodeList = applicationRecordDetailEntities.stream().
                    map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
            //获取发票号码集合
            List<String> invoiceNoList = applicationRecordDetailEntities.stream().
                    map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
            QueryWrapper<InvoiceCollectionEntity> invoiceQuery = new QueryWrapper<>();
            invoiceQuery.in("invoice_code", invoiceCodeList);
            invoiceQuery.in("invoice_no", invoiceNoList);
            IPage<InvoiceCollectionEntity> invoiceResult = PageHelp.siamplePage(invoiceCollectionService, invoiceQuery, page, InvoiceCollectionEntity.class);
            return Response.success(invoiceResult);
        } else if (CodeConstant.TICKET_LIST.contains(ticketTypes)) {
            if (CodeConstant.TICKET_HCP.equals(ticketTypes)) {
                //火车票
                List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = getRecordDetail(recordNo);
                //获取发票号码集合
                List<String> invoiceNoList = applicationRecordDetailEntities.stream().
                        map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                QueryWrapper<TrainTicketEntity> trainQuery = new QueryWrapper<>();
                trainQuery.in("invoice_no", invoiceNoList);
                IPage<TrainTicketEntity> trainTicketResult = PageHelp.siamplePage(trainTicketCollectService, trainQuery, page, TrainTicketEntity.class);
                return Response.success(trainTicketResult);
            } else if (CodeConstant.TICKET_CZP.equals(ticketTypes)) {
                //出租车发票
                List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = getRecordDetail(recordNo);
                //获取发票代码集合
                List<String> invoiceCodeList = applicationRecordDetailEntities.stream().
                        map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
                //获取发票号码集合
                List<String> invoiceNoList = applicationRecordDetailEntities.stream().
                        map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                QueryWrapper<TaxiTicketEntity> taxiQuery = new QueryWrapper<>();
                taxiQuery.in("invoice_code", invoiceCodeList);
                taxiQuery.in("invoice_no", invoiceNoList);
                IPage<TaxiTicketEntity> taxiTicketResult = PageHelp.siamplePage(taxiTicketCollectService, taxiQuery, page, TaxiTicketEntity.class);
                return Response.success(taxiTicketResult);
            } else if (CodeConstant.TICKET_FJP.equals(ticketTypes)) {
                //飞机行程单
                List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = getRecordDetail(recordNo);
                //获取发票号码集合
                List<String> invoiceNoList = applicationRecordDetailEntities.stream().
                        map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                QueryWrapper<PlaneTicketEntity> planeQuery = new QueryWrapper<>();
                planeQuery.in("ticket_no", invoiceNoList);
                IPage<PlaneTicketEntity> planeTicketResult = PageHelp.siamplePage(planeTicketCollectService, planeQuery, page, PlaneTicketEntity.class);
                return Response.success(planeTicketResult);
            } else if (CodeConstant.TICKET_QCP.equals(ticketTypes)) {
                //客运汽车票
                List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = getRecordDetail(recordNo);
                //获取发票代码集合
                List<String> invoiceCodeList = applicationRecordDetailEntities.stream().
                        map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
                //获取发票号码集合
                List<String> invoiceNoList = applicationRecordDetailEntities.stream().
                        map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                QueryWrapper<BusTicketEntity> busQuery = new QueryWrapper<>();
                busQuery.in("invoice_code", invoiceCodeList);
                busQuery.in("invoice_no", invoiceNoList);
                IPage<BusTicketEntity> busTicketResult = PageHelp.siamplePage(busTicketCollectService, busQuery, page, BusTicketEntity.class);
                return Response.success(busTicketResult);
            } else if (CodeConstant.TICKET_DEFP.equals(ticketTypes)) {
                //定额发票
                List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = getRecordDetail(recordNo);
                //获取发票代码集合
                List<String> invoiceCodeList = applicationRecordDetailEntities.stream().
                        map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
                //获取发票号码集合
                List<String> invoiceNoList = applicationRecordDetailEntities.stream().
                        map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                QueryWrapper<QuotaTicketEntity> quotaQuery = new QueryWrapper<>();
                quotaQuery.in("invoice_code", invoiceCodeList);
                quotaQuery.in("invoice_no", invoiceNoList);
                IPage<QuotaTicketEntity> quotaTicketResult = PageHelp.siamplePage(quotaTicketCollectService, quotaQuery, page, QuotaTicketEntity.class);
                return Response.success(quotaTicketResult);
            } else if (CodeConstant.TICKET_JDFP.equals(ticketTypes)) {
                //机打发票
                List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = getRecordDetail(recordNo);
                //获取发票代码集合
                List<String> invoiceCodeList = applicationRecordDetailEntities.stream().
                        map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
                //获取发票号码集合
                List<String> invoiceNoList = applicationRecordDetailEntities.stream().
                        map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                QueryWrapper<MachinTicketEntity> machinQuery = new QueryWrapper<>();
                machinQuery.in("invoice_code", invoiceCodeList);
                machinQuery.in("invoice_no", invoiceNoList);
                IPage<MachinTicketEntity> machinTicketResult = PageHelp.siamplePage(machinTicketCollectService, machinQuery, page, MachinTicketEntity.class);
                return Response.success(machinTicketResult);
            } else if (CodeConstant.TICKET_GLFP.equals(ticketTypes)) {
                //过路费发票
                List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = getRecordDetail(recordNo);
                //获取发票代码集合
                List<String> invoiceCodeList = applicationRecordDetailEntities.stream().
                        map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
                //获取发票号码集合
                List<String> invoiceNoList = applicationRecordDetailEntities.stream().
                        map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                QueryWrapper<TollTicketEntity> tollQuery = new QueryWrapper<>();
                tollQuery.in("invoice_code", invoiceCodeList);
                tollQuery.in("invoice_no", invoiceNoList);
                IPage<TollTicketEntity> tollTicketResult = PageHelp.siamplePage(tollTicketCollectService, tollQuery, page, TollTicketEntity.class);
                return Response.success(tollTicketResult);
            }
        } else {
            return Response.error("PUR_104");
        }
        return Response.success();
    }

    /**
     * 撤销申请【申请记录】
     *
     * @param applicationRecordDTO
     * @return
     */
    public Response cancelRecord(ApplicationRecordDTO applicationRecordDTO) {
        QueryWrapper<ApplicationRecordEntity> recordQuery = new QueryWrapper<>();
        recordQuery.eq("record_no", applicationRecordDTO.getRecordNo());
        recordQuery.in("approval_status", CodeConstant.NOT_CANCEL_LIST);
        if (baseMapper.selectCount(recordQuery) > 0) {
            return Response.error("PUR_106");
        }
        String ticketTypes = applicationRecordDTO.getTicketTypes();
        QueryWrapper<ApplicationRecordDetailEntity> recordDetailQuery = new QueryWrapper<>();
        //获取
        recordDetailQuery.eq("record_no", applicationRecordDTO.getRecordNo());
        //recordDetailQuery.eq("approval_status", CodeConstant.APPROVAL_1);
        if(StrUtil.isNotBlank(ticketTypes) ){
            if("00".equals(ticketTypes)){
                recordDetailQuery.in("invoice_type", CodeConstant.INVOICE_LIST);
            }else {
                recordDetailQuery.eq("invoice_type", ticketTypes);
            }
        }else {
            return Response.error("PUR_107");
        }
        List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = applicationRecordDetailMapper.selectList(recordDetailQuery);
        if(applicationRecordDetailEntities == null || applicationRecordDetailEntities.isEmpty()){
            return Response.error("PUR_104");
        }
        if (CodeConstant.TICKET_TYPES.equals(ticketTypes)) {
            //获取发票代码集合
            List<String> invoiceCodeList = applicationRecordDetailEntities.stream().
                    map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
            //获取发票号码集合
            List<String> invoiceNoList = applicationRecordDetailEntities.stream().
                    map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
            UpdateWrapper<InvoiceCollectionEntity> invoiceUpdate = new UpdateWrapper<>();
            invoiceUpdate.in("invoice_code", invoiceCodeList);
            invoiceUpdate.in("invoice_no", invoiceNoList);
            InvoiceCollectionEntity invoiceCollectionEntity = new InvoiceCollectionEntity();
            //根据代码号码更新发票报销状态为已报销
            invoiceCollectionEntity.setReimbursementStatus(CodeConstant.REIMBURSEMENT_2);
            invoiceCollectionEntity.setProcedureStatus(CodeConstant.APPROVAL_4);
            invoiceCollectionMapper.update(invoiceCollectionEntity, invoiceUpdate);
        } else if (CodeConstant.TICKET_LIST.contains(ticketTypes)) {
            if (CodeConstant.TICKET_HCP.equals(ticketTypes)) {
                //获取发票号码集合<火车票只有号码，没有代码>
                List<String> invoiceNoList = applicationRecordDetailEntities.stream().map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                UpdateWrapper<TrainTicketEntity> trainUpdate = new UpdateWrapper<>();
                trainUpdate.in("invoice_no", invoiceNoList);
                TrainTicketEntity trainTicketEntity = new TrainTicketEntity();
                trainTicketEntity.setReimbursementStatus(CodeConstant.REIMBURSEMENT_2);
                trainTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_4);
                trainTicketMapper.update(trainTicketEntity, trainUpdate);
            } else if (CodeConstant.TICKET_CZP.equals(ticketTypes)) {
                //获取发票代码集合
                List<String> invoiceCodeList = applicationRecordDetailEntities.stream().map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
                //获取发票号码集合
                List<String> invoiceNoList = applicationRecordDetailEntities.stream().map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                UpdateWrapper<TaxiTicketEntity> taxiUpdate = new UpdateWrapper<>();
                taxiUpdate.in("invoice_no", invoiceNoList);
                taxiUpdate.in("invoice_code", invoiceCodeList);
                TaxiTicketEntity taxiTicketEntity = new TaxiTicketEntity();
                taxiTicketEntity.setReimbursementStatus(CodeConstant.REIMBURSEMENT_2);
                taxiTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_4);
                taxiTicketMapper.update(taxiTicketEntity, taxiUpdate);
            } else if (CodeConstant.TICKET_FJP.equals(ticketTypes)) {
                //获取电子客票号码集合
                List<String> invoiceNoList = applicationRecordDetailEntities.stream().map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                UpdateWrapper<PlaneTicketEntity> planeUpdate = new UpdateWrapper<>();
                planeUpdate.in("ticket_no", invoiceNoList);
                PlaneTicketEntity planeTicketEntity = new PlaneTicketEntity();
                planeTicketEntity.setReimbursementStatus(CodeConstant.REIMBURSEMENT_2);
                planeTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_4);
                planeTicketMapper.update(planeTicketEntity, planeUpdate);
            } else if (CodeConstant.TICKET_QCP.equals(ticketTypes)) {
                //获取发票代码集合
                List<String> invoiceCodeList = applicationRecordDetailEntities.stream().map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
                //获取发票号码集合
                List<String> invoiceNoList = applicationRecordDetailEntities.stream().map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                UpdateWrapper<BusTicketEntity> busUpdate = new UpdateWrapper<>();
                busUpdate.in("invoice_code", invoiceCodeList);
                busUpdate.in("invoice_no", invoiceNoList);
                BusTicketEntity busTicketEntity = new BusTicketEntity();
                busTicketEntity.setReimbursementStatus(CodeConstant.REIMBURSEMENT_2);
                busTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_4);
                busTicketMapper.update(busTicketEntity, busUpdate);
            } else if (CodeConstant.TICKET_DEFP.equals(ticketTypes)) {
                //获取发票代码集合
                List<String> invoiceCodeList = applicationRecordDetailEntities.stream().map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
                //获取发票号码集合
                List<String> invoiceNoList = applicationRecordDetailEntities.stream().map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                UpdateWrapper<QuotaTicketEntity> quotaUpdate = new UpdateWrapper<>();
                quotaUpdate.in("invoice_code", invoiceCodeList);
                quotaUpdate.in("invoice_no", invoiceNoList);
                QuotaTicketEntity quotaTicketEntity = new QuotaTicketEntity();
                quotaTicketEntity.setReimbursementStatus(CodeConstant.REIMBURSEMENT_2);
                quotaTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_4);
                quotaTicketMapper.update(quotaTicketEntity, quotaUpdate);
            } else if (CodeConstant.TICKET_JDFP.equals(ticketTypes)) {
                //获取发票代码集合
                List<String> invoiceCodeList = applicationRecordDetailEntities.stream().map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
                //获取发票号码集合
                List<String> invoiceNoList = applicationRecordDetailEntities.stream().map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                UpdateWrapper<MachinTicketEntity> machinUpdate = new UpdateWrapper<>();
                machinUpdate.in("invoice_code", invoiceCodeList);
                machinUpdate.in("invoice_no", invoiceNoList);
                MachinTicketEntity machinTicketEntity = new MachinTicketEntity();
                machinTicketEntity.setReimbursementStatus(CodeConstant.REIMBURSEMENT_2);
                machinTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_4);
                machinTicketMapper.update(machinTicketEntity, machinUpdate);
            } else if (CodeConstant.TICKET_GLFP.equals(ticketTypes)) {
                //获取发票代码集合
                List<String> invoiceCodeList = applicationRecordDetailEntities.stream().map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
                //获取发票号码集合
                List<String> invoiceNoList = applicationRecordDetailEntities.stream().map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                UpdateWrapper<TollTicketEntity> tollUpdate = new UpdateWrapper<>();
                tollUpdate.in("invoice_code", invoiceCodeList);
                tollUpdate.in("invoice_no", invoiceNoList);
                TollTicketEntity tollTicketEntity = new TollTicketEntity();
                tollTicketEntity.setReimbursementStatus(CodeConstant.REIMBURSEMENT_2);
                tollTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_4);
                tollTicketMapper.update(tollTicketEntity, tollUpdate);
            }
        } else {
            return Response.error("PUR_104");
        }
        //根据申请单号，将申请状态更新为已撤销
        UpdateWrapper<ApplicationRecordEntity> recordUpdate = new UpdateWrapper<>();
        recordUpdate.eq("record_no", applicationRecordDTO.getRecordNo());
        ApplicationRecordEntity applicationRecordEntity = new ApplicationRecordEntity();
        applicationRecordEntity.setApprovalStatus(CodeConstant.APPROVAL_4);
        baseMapper.update(applicationRecordEntity, recordUpdate);
        return Response.success();
    }

    /**
     * 批量审批【审批驳回】
     *
     * @param applicationRecordDTOs
     * @return
     */
    public Response approval(List<ApplicationRecordDTO> applicationRecordDTOs) {
        //全部数据是1才能审批
        QueryWrapper<ApplicationRecordEntity> recordQuery = new QueryWrapper<>();
         List<String> recordNoList = applicationRecordDTOs.stream().map(ApplicationRecordDTO::getRecordNo).collect(Collectors.toList());
        recordQuery.in("record_no", recordNoList);
        recordQuery.in("approval_status", CodeConstant.NOT_EXAMINE_LIST);
        if (baseMapper.selectCount(recordQuery) > 0) {
            return Response.error("PUR_111");
        }
        for (ApplicationRecordDTO applicationRecordDTO : applicationRecordDTOs) {
            String ticketTypes = applicationRecordDTO.getTicketTypes();
            String recordNo = applicationRecordDTO.getRecordNo();
            String approvalStatus = applicationRecordDTO.getApprovalStatus();
            if (CodeConstant.TICKET_TYPES.equals(ticketTypes)) {
                InvoiceCollectionEntity invoiceCollectionEntity = new InvoiceCollectionEntity();
                List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = getRecordDetail(recordNo);
                //获取发票代码集合
                List<String> invoiceCodeList = applicationRecordDetailEntities.stream().
                        map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
                //获取发票号码集合
                List<String> invoiceNoList = applicationRecordDetailEntities.stream().
                        map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                UpdateWrapper<InvoiceCollectionEntity> invoiceUpdate = new UpdateWrapper<>();
                //更新采集表数据为未报销
                invoiceUpdate.in("invoice_code", invoiceCodeList);
                invoiceUpdate.in("invoice_no", invoiceNoList);
                //审批不通过，数据状态不变，更新申请记录为已驳回
                Response message = updateApprovalStatus(applicationRecordDTO);
                if (message.getCode().equals("PUR_111")) {
                    return Response.error("PUR_111");
                }
                if (CodeConstant.APPROVAL_2.equals(approvalStatus)) {
                    //根据代码号码更新发票报销状态为报销驳回销
                    invoiceCollectionEntity.setReimbursementStatus(CodeConstant.REIMBURSEMENT_3);
                    invoiceCollectionEntity.setProcedureStatus(CodeConstant.APPROVAL_1);
                    invoiceCollectionMapper.update(invoiceCollectionEntity, invoiceUpdate);
                    QueryWrapper<InvoicePoolEntity> invoicePoolQuery = new QueryWrapper<>();
                    invoicePoolQuery.in("invoice_code", invoiceCodeList);
                    invoicePoolQuery.in("invoice_no", invoiceNoList);
                    //删除发票池中的数据
                    invoicePoolMapper.delete(invoicePoolQuery);
                } else if (CodeConstant.APPROVAL_3.equals(approvalStatus)) {
                    invoiceCollectionEntity.setProcedureStatus(CodeConstant.APPROVAL_3);
                    invoiceCollectionMapper.update(invoiceCollectionEntity, invoiceUpdate);
                }
            } else if (CodeConstant.TICKET_LIST.contains(ticketTypes)) {
                if (CodeConstant.TICKET_HCP.equals(ticketTypes)) {
                    List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = getRecordDetail(recordNo);
                    //获取发票号码集合<火车票只有号码，没有代码>
                    List<String> invoiceNoList = applicationRecordDetailEntities.stream().
                            map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                    UpdateWrapper<TrainTicketEntity> trainUpdate = new UpdateWrapper<>();
                    trainUpdate.in("invoice_no", invoiceNoList);
                    TrainTicketEntity trainTicketEntity = new TrainTicketEntity();
                    //审批通过，数据回到未报销，并且删除已经入池数据
                    Response message = updateApprovalStatus(applicationRecordDTO);
                    if (message.getCode().equals("PUR_111")) {
                        return Response.error("PUR_111");
                    }
                    if (CodeConstant.APPROVAL_2.equals(approvalStatus)) {
                        //根据号码更新发票报销状态为报销驳回、审批状态为审批驳回
                        trainTicketEntity.setReimbursementStatus(CodeConstant.REIMBURSEMENT_3);
                        trainTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_1);
                        trainTicketMapper.update(trainTicketEntity, trainUpdate);
                        QueryWrapper<TrainTicketPoolEntity> trainPoolQuery = new QueryWrapper<>();
                        trainPoolQuery.in("invoice_no", invoiceNoList);
                        //删除火车票发票池中的数据
                        trainTicketPoolMapper.delete(trainPoolQuery);
                    } else if (CodeConstant.APPROVAL_3.equals(approvalStatus)) {
                        trainTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_3);
                        trainTicketMapper.update(trainTicketEntity, trainUpdate);
                    }
                } else if (CodeConstant.TICKET_CZP.equals(ticketTypes)) {
                    List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = getRecordDetail(recordNo);
                    //获取发票代码集合
                    List<String> invoiceCodeList = applicationRecordDetailEntities.stream().
                            map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
                    //获取发票号码集合
                    List<String> invoiceNoList = applicationRecordDetailEntities.stream().
                            map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                    UpdateWrapper<TaxiTicketEntity> taxiUpdate = new UpdateWrapper<>();
                    taxiUpdate.in("invoice_no", invoiceNoList);
                    taxiUpdate.in("invoice_code", invoiceCodeList);
                    TaxiTicketEntity taxiTicketEntity = new TaxiTicketEntity();
                    //审批通过，数据回到未报销，并且删除已经入池数据
                    Response message = updateApprovalStatus(applicationRecordDTO);
                    if (message.getCode().equals("PUR_111")) {
                        return Response.error("PUR_111");
                    }
                    if (CodeConstant.APPROVAL_2.equals(approvalStatus)) {
                        //根据号码更新发票报销状态为报销驳回、审批状态为审批驳回
                        taxiTicketEntity.setReimbursementStatus(CodeConstant.REIMBURSEMENT_3);
                        taxiTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_1);
                        taxiTicketMapper.update(taxiTicketEntity, taxiUpdate);
                        QueryWrapper<TaxiTicketPoolEntity> taxiPoolQuery = new QueryWrapper<>();
                        taxiPoolQuery.in("invoice_no", invoiceNoList);
                        taxiPoolQuery.in("invoice_code", invoiceCodeList);
                        //删除出租车发票池中的数据
                        taxiTicketPoolMapper.delete(taxiPoolQuery);
                    } else if (CodeConstant.APPROVAL_3.equals(approvalStatus)) {
                        taxiTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_3);
                        taxiTicketMapper.update(taxiTicketEntity, taxiUpdate);
                    }
                } else if (CodeConstant.TICKET_QCP.equals(ticketTypes)) {
                    List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = getRecordDetail(recordNo);
                    //获取发票代码集合
                    List<String> invoiceCodeList = applicationRecordDetailEntities.stream().
                            map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
                    //获取发票号码集合
                    List<String> invoiceNoList = applicationRecordDetailEntities.stream().
                            map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                    UpdateWrapper<BusTicketEntity> busUpdate = new UpdateWrapper<>();
                    busUpdate.in("invoice_no", invoiceNoList);
                    busUpdate.in("invoice_code", invoiceCodeList);
                    BusTicketEntity busTicketEntity = new BusTicketEntity();
                    //审批通过，数据回到未报销，并且删除已经入池数据
                    Response message = updateApprovalStatus(applicationRecordDTO);
                    if (message.getCode().equals("PUR_111")) {
                        return Response.error("PUR_111");
                    }
                    if (CodeConstant.APPROVAL_2.equals(approvalStatus)) {
                        //根据号码更新发票报销状态为报销驳回、审批状态为审批驳回
                        busTicketEntity.setReimbursementStatus(CodeConstant.REIMBURSEMENT_3);
                        busTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_1);
                        busTicketMapper.update(busTicketEntity, busUpdate);
                        QueryWrapper<BusTicketPoolEntity> busPoolQuery = new QueryWrapper<>();
                        busPoolQuery.in("invoice_no", invoiceNoList);
                        busPoolQuery.in("invoice_code", invoiceCodeList);
                        //删除汽车票池中的数据
                        busTicketPoolMapper.delete(busPoolQuery);
                    } else if (CodeConstant.APPROVAL_3.equals(approvalStatus)) {
                        busTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_3);
                        busTicketMapper.update(busTicketEntity, busUpdate);
                    }
                } else if (CodeConstant.TICKET_DEFP.equals(ticketTypes)) {
                    List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = getRecordDetail(recordNo);
                    //获取发票代码集合
                    List<String> invoiceCodeList = applicationRecordDetailEntities.stream().
                            map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
                    //获取发票号码集合
                    List<String> invoiceNoList = applicationRecordDetailEntities.stream().
                            map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                    UpdateWrapper<QuotaTicketEntity> quotaUpdate = new UpdateWrapper<>();
                    quotaUpdate.in("invoice_no", invoiceNoList);
                    quotaUpdate.in("invoice_code", invoiceCodeList);
                    QuotaTicketEntity quotaTicketEntity = new QuotaTicketEntity();
                    //审批通过，数据回到未报销，并且删除已经入池数据
                    Response message = updateApprovalStatus(applicationRecordDTO);
                    if (message.getCode().equals("PUR_111")) {
                        return Response.error("PUR_111");
                    }
                    if (CodeConstant.APPROVAL_2.equals(approvalStatus)) {
                        //根据号码更新发票报销状态为报销驳回、审批状态为审批驳回
                        quotaTicketEntity.setReimbursementStatus(CodeConstant.REIMBURSEMENT_3);
                        quotaTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_1);
                        quotaTicketMapper.update(quotaTicketEntity, quotaUpdate);
                        QueryWrapper<QuotaTicketPoolEntity> quatoPoolQuery = new QueryWrapper<>();
                        quatoPoolQuery.in("invoice_no", invoiceNoList);
                        quatoPoolQuery.in("invoice_code", invoiceCodeList);
                        //删除定额发票池中的数据
                        quotaTicketPoolMapper.delete(quatoPoolQuery);
                    } else if (CodeConstant.APPROVAL_3.equals(approvalStatus)) {
                        quotaTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_3);
                        quotaTicketMapper.update(quotaTicketEntity, quotaUpdate);
                    }
                } else if (CodeConstant.TICKET_GLFP.equals(ticketTypes)) {
                    List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = getRecordDetail(recordNo);
                    //获取发票代码集合
                    List<String> invoiceCodeList = applicationRecordDetailEntities.stream().
                            map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
                    //获取发票号码集合
                    List<String> invoiceNoList = applicationRecordDetailEntities.stream().
                            map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                    UpdateWrapper<TollTicketEntity> tollUpdate = new UpdateWrapper<>();
                    tollUpdate.in("invoice_no", invoiceNoList);
                    tollUpdate.in("invoice_code", invoiceCodeList);
                    TollTicketEntity tollTicketEntity = new TollTicketEntity();
                    //审批通过，数据回到未报销，并且删除已经入池数据
                    Response message = updateApprovalStatus(applicationRecordDTO);
                    if (message.getCode().equals("PUR_111")) {
                        return Response.error("PUR_111");
                    }
                    if (CodeConstant.APPROVAL_2.equals(approvalStatus)) {
                        //根据号码更新发票报销状态为报销驳回、审批状态为审批驳回
                        tollTicketEntity.setReimbursementStatus(CodeConstant.REIMBURSEMENT_3);
                        tollTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_1);
                        tollTicketMapper.update(tollTicketEntity, tollUpdate);
                        QueryWrapper<TollTicketPoolEntity> tollPoolQuery = new QueryWrapper<>();
                        tollPoolQuery.in("invoice_no", invoiceNoList);
                        tollPoolQuery.in("invoice_code", invoiceCodeList);
                        //删除过路费发票池中的数据
                        tollTicketPoolMapper.delete(tollPoolQuery);
                    } else if (CodeConstant.APPROVAL_3.equals(approvalStatus)) {
                        tollTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_3);
                        tollTicketMapper.update(tollTicketEntity, tollUpdate);
                    }
                } else if (CodeConstant.TICKET_JDFP.equals(ticketTypes)) {
                    List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = getRecordDetail(recordNo);
                    //获取发票代码集合
                    List<String> invoiceCodeList = applicationRecordDetailEntities.stream().
                            map(ApplicationRecordDetailEntity::getInvoiceCode).collect(Collectors.toList());
                    //获取发票号码集合
                    List<String> invoiceNoList = applicationRecordDetailEntities.stream().
                            map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                    UpdateWrapper<MachinTicketEntity> machinUpdate = new UpdateWrapper<>();
                    machinUpdate.in("invoice_no", invoiceNoList);
                    machinUpdate.in("invoice_code", invoiceCodeList);
                    MachinTicketEntity machinTicketEntity = new MachinTicketEntity();
                    //审批通过，数据回到未报销，并且删除已经入池数据
                    Response message = updateApprovalStatus(applicationRecordDTO);
                    if (message.getCode().equals("PUR_111")) {
                        return Response.error("PUR_111");
                    }
                    if (CodeConstant.APPROVAL_2.equals(approvalStatus)) {
                        //根据号码更新发票报销状态为报销驳回、审批状态为审批驳回
                        machinTicketEntity.setReimbursementStatus(CodeConstant.REIMBURSEMENT_3);
                        machinTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_1);
                        machinTicketMapper.update(machinTicketEntity, machinUpdate);
                        QueryWrapper<MachinTicketPoolEntity> machinPoolQuery = new QueryWrapper<>();
                        machinPoolQuery.in("invoice_no", invoiceNoList);
                        machinPoolQuery.in("invoice_code", invoiceCodeList);
                        //删除机打发票池中的数据
                        machinTicketPoolMapper.delete(machinPoolQuery);
                    } else if (CodeConstant.APPROVAL_3.equals(approvalStatus)) {
                        machinTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_3);
                        machinTicketMapper.update(machinTicketEntity, machinUpdate);
                    }
                } else if (CodeConstant.TICKET_FJP.equals(ticketTypes)) {
                    List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = getRecordDetail(recordNo);
                    //获取电子客票号码集合
                    List<String> invoiceNoList = applicationRecordDetailEntities.stream().
                            map(ApplicationRecordDetailEntity::getInvoiceNo).collect(Collectors.toList());
                    UpdateWrapper<PlaneTicketEntity> planeUpdate = new UpdateWrapper<>();
                    planeUpdate.in("ticket_no", invoiceNoList);
                    PlaneTicketEntity planeTicketEntity = new PlaneTicketEntity();
                    Response message = updateApprovalStatus(applicationRecordDTO);
                    if (message.getCode().equals("PUR_111")) {
                        return Response.error("PUR_111");
                    }
                    if (CodeConstant.APPROVAL_2.equals(approvalStatus)) {
                        //审批通过，数据回到未报销，并且删除已经入池数据
                        //根据电子客票号码更新发票报销状态为报销驳回、审批状态为审批驳回
                        planeTicketEntity.setReimbursementStatus(CodeConstant.REIMBURSEMENT_3);
                        planeTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_1);
                        planeTicketMapper.update(planeTicketEntity, planeUpdate);
                        QueryWrapper<PlaneTicketPoolEntity> planePoolQuery = new QueryWrapper<>();
                        planePoolQuery.in("ticket_no", invoiceNoList);
                        //删除飞机客票行程单池中的数据
                        planeTicketPoolMapper.delete(planePoolQuery);
                    } else if (CodeConstant.APPROVAL_3.equals(approvalStatus)) {
                        planeTicketEntity.setProcedureStatus(CodeConstant.APPROVAL_3);
                        planeTicketMapper.update(planeTicketEntity, planeUpdate);
                    }
                }
            } else {
                return Response.error("PUR_104");
            }
        }
        return Response.success();
    }

    //更新审批状态
    private Response updateApprovalStatus(ApplicationRecordDTO applicationRecordDTO) {
        String recordNo = applicationRecordDTO.getRecordNo();
        String approvalStatus = applicationRecordDTO.getApprovalStatus();
        String approvalComments = applicationRecordDTO.getApprovalComments();
        if(StrUtil.isBlank(recordNo) || StrUtil.isBlank(approvalStatus) || StrUtil.isBlank(approvalComments) ){
            return Response.error("PUR_111");
        }
        UpdateWrapper<ApplicationRecordEntity> recordUpdate = new UpdateWrapper<>();
        recordUpdate.eq("record_no", recordNo);
        ApplicationRecordEntity applicationRecordEntity = new ApplicationRecordEntity();
        applicationRecordEntity.setApprovalStatus(approvalStatus);
        applicationRecordEntity.setApprovalComments(approvalComments);
        applicationRecordEntity.setApprovalUser(UserInfoUtil.getUserNickName());
        applicationRecordEntity.setApprovalUserId(UserInfoUtil.getUserID());
        baseMapper.update(applicationRecordEntity, recordUpdate);
        return Response.success();
    }

    //根据申请编号获取关联发票信息
    private List<ApplicationRecordDetailEntity> getRecordDetail(String recordNo) {
        QueryWrapper<ApplicationRecordDetailEntity> recordDetailQuery = new QueryWrapper<>();
        recordDetailQuery.eq("record_no", recordNo);
        List<ApplicationRecordDetailEntity> applicationRecordDetailEntities = applicationRecordDetailMapper.selectList(recordDetailQuery);
        return applicationRecordDetailEntities;
    }
}
